package com.jerry.template.module.mainpage.shouye.duanzi.adapter;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.TextView;

import com.jerry.template.R;
import com.jerry.template.base.BaseApplication;
import com.jerry.template.module.mainpage.shouye.duanzi.activity.ActivityJokeDetailActivity;
import com.jerry.template.module.mainpage.shouye.duanzi.entity.TAGDetail;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import de.hdodenhof.circleimageview.CircleImageView;

public class ActivityTagJokeListContentAdapter<T> extends BaseAdapter {

    private List<TAGDetail.DataBean.DataBean1> groupBean = new ArrayList<>();

    private ImageLoader imageLoader;
    private Context context;
    private LayoutInflater layoutInflater;
    private DisplayImageOptions options;
    private int duanziDigg;
    private long groupId;
    private long userId;

    public ActivityTagJokeListContentAdapter(Context context, List<TAGDetail.DataBean.DataBean1> groupBean) {
        this.context = context;
        this.groupBean = groupBean;
        this.layoutInflater = LayoutInflater.from(context);
        imageLoader = BaseApplication.getApplication().getImageLoader();
        options = BaseApplication.getApplication().getImageOptions();
    }

    @Override
    public int getCount() {
        return groupBean.size();
    }

    @Override
    public TAGDetail.DataBean.DataBean1 getItem(int position) {
        return groupBean.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.activity_tag_joke_list_content, null);
            viewHolder = new ViewHolder();
            ButterKnife.bind(viewHolder, convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = new ViewHolder();
            viewHolder = (ViewHolder) convertView.getTag();
        }
        TAGDetail.DataBean.DataBean1.GroupBean userBean = getItem(position).getGroup();

        viewHolder.userName.setText(userBean.getUser().getName() + "");

        viewHolder.duanziContent.setText(userBean.getContent() + "");

        viewHolder.btnBury.setText(userBean.getBury_count() + "");

        viewHolder.btnDigg.setText(userBean.getDigg_count() + "");

        viewHolder.btnCommenticon.setText(userBean.getComment_count() + "");

        viewHolder.btnJokeShare.setText(userBean.getShare_count() + "");



        String userName = userBean.getUser().getName() + "";
        String duanziContent = userBean.getContent() + "";
        duanziDigg = userBean.getDigg_count();
        String duanziBury = userBean.getBury_count() + "";
        String duanziCommenticon = userBean.getComment_count() + "";
        String userHead = userBean.getUser().getAvatar_url() + "";
        groupId = userBean.getGroup_id();
        userId = userBean.getUser().getUser_id();

        // 图片显示
        imageLoader.displayImage(
                userBean.getUser().getAvatar_url(),
                viewHolder.userHead,
                options);

        // 设置事件
        convertView.setOnClickListener(
                new JokeItemClickListener(userName,duanziContent, duanziDigg,duanziBury,duanziCommenticon,userHead));

        return convertView;
    }

    /**
     * 自定义点击事件
     */
    private class JokeItemClickListener implements View.OnClickListener {
        private String userName;
        private String duanziContent;
        private int duanziDigg;
        private String duanziBury;
        private String duanziCommenticon;
        private String userHead;

        public JokeItemClickListener(String userName,String duanziContent,
                                     int duanziDigg,String duanziBury,String duanziCommenticon,String userHead) {
            this.userName = userName;
            this.duanziContent = duanziContent;
            this.duanziDigg = duanziDigg;
            this.duanziBury = duanziBury;
            this.duanziCommenticon = duanziCommenticon;
            this.userHead = userHead;
        }

        @Override
        public void onClick(View v) {
            Intent intent = new Intent();
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            intent.setClass(context, ActivityJokeDetailActivity.class);
            intent.putExtra("userName", userName);
            intent.putExtra("duanziContent", duanziContent);
            intent.putExtra("duanziDigg", duanziDigg);
            intent.putExtra("duanziBury", duanziBury);
            intent.putExtra("duanziCommenticon", duanziCommenticon);
            intent.putExtra("userHead", userHead);
            intent.putExtra("groupId",groupId);
            intent.putExtra("userId",userId);
            context.startActivity(intent);
        }
    }

    protected class ViewHolder {
        @BindView(R.id.user_head)
        CircleImageView userHead;
        @BindView(R.id.user_name)
        TextView userName;
        @BindView(R.id.content)
        TextView duanziContent;
        @BindView(R.id.btn_digg)
        Button btnDigg;
        @BindView(R.id.btn_bury)
        Button btnBury;
        @BindView(R.id.btn_commenticon)
        Button btnCommenticon;
        @BindView(R.id.btn_joke_share)
        Button btnJokeShare;

    }
}
